package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.CustomerReportQuery;
import cn.wolfcode.crm.service.ICustomerReportService;
import cn.wolfcode.crm.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
        PageInfo pageInfo = customerReportService.selectByGroup(qo);
        model.addAttribute("result", pageInfo);
        return "/customerReport/list";
    }

    @RequestMapping("chartByBar")
    public String chartByBar(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
        List<Map> list = customerReportService.listAll(qo);
        List xList = new ArrayList();
        List yList = new ArrayList();
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //需要显示  ["赵总","张三"]  加上字符串
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        String groupName = MessageUtil.changMsg(qo);
        //每个group 的名字
        model.addAttribute("groupName", groupName);
        return "/customerReport/listByBar";
    }

    @RequestMapping("chartByPie")
    public String chartByPie(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
        List<Map> list = customerReportService.listAll(qo);
        List xList = new ArrayList();
        List yList = new ArrayList();
        for (Map map : list) {
            Map<String, Object> map1 = new HashMap();
            map1.put("value", map.get("number"));
            map1.put("name", map.get("groupType"));
            yList.add(map1);
            xList.add(map.get("groupType"));
        }
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        //每个group 的名字
        String groupName = MessageUtil.changMsg(qo);
        model.addAttribute("groupName", groupName);
        return "/customerReport/listByPie";
    }
}
